package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.dict.CommonDict;
import entities.TBin;
import interfaces.IBinService;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.StringUtil;

public class BinAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private IBinService binService;

	private TBin binBean;
	private List<TBin> binList;

	private String binNo;
	private String binNos;
	private String binType;
	private String binCatalog;
	private String binName;
	private String binCountry;
	private String binState;
	private Date binDate;
	private Date binDateTop;
	private Date binDateEnd;
	private String binRemark;

	// 展示对象
	public String show() throws Exception {
		if (!binService.exist(binNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			binBean = binService.fetch(binNo);
			return SUCCESS;
		}
	}

	// 编辑对象
	public String edit() throws Exception {
		if (!binService.exist(binNo)) {
			binBean = new TBin();
			binBean.setBinState("1");
		} else {
			binBean = binService.fetch(binNo);
		}
		return SUCCESS;
	}

	// 持久化对象
	public String save() throws Exception {
		if (binService.exist(binNo)) {
			addActionError("该BIN已存在");
			return INFO;
		}
		binBean = new TBin();
		binBean.setBinNo(binNo);
		binBean.setBinType(binType);
		binBean.setBinCatalog(binCatalog);
		binBean.setBinName(binName);
		binBean.setBinCountry(binCountry);
		binBean.setBinState(binState);
		binBean.setBinDate(new Date());
		binBean.setBinRemark(binRemark);
		binService.persist(binBean);
		return SUCCESS;
	}

	// 批量删除
	public String move() throws Exception {
		for (String binNo : StringUtil.split(binNos)) {
			binService.remove(binNo);
		}
		return SUCCESS;
	}

	// 列表展示
	// paramMap为查询条件,sortInfo为排序方式,pageInfo为分页对象
	// paramMap可为空,sortInfo可为空,pageInfo不能为空,如果需要查询全部则设置pageIndex为1,pageSize为Integer.MAX_VALUE
	public String list() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("binNo", StringUtil.split(binNo));
		paramMap.put("binCountry", binCountry);
		paramMap.put("binState", binState);
		paramMap.put("binDate", ParameterUtil.dateCombin(binDateTop, binDateEnd));

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "binDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TBin> resultSet = binService.search(querySet);
		binList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	// 导出数据 查询条件等同于列表查询条件
	public String export() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("binNo", StringUtil.split(binNo));
		paramMap.put("binCountry", binCountry);
		paramMap.put("binState", binState);
		paramMap.put("binDate", ParameterUtil.dateCombin(binDateTop, binDateEnd));

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "binDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TBin> resultSet = binService.search(querySet);
		binList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("卡BIN编号");
		fieldHead.add("卡类别");
		fieldHead.add("卡种");
		fieldHead.add("卡所属银行名称");
		fieldHead.add("卡所属国家");
		fieldHead.add("状态");
		fieldHead.add("更新日期日期");
		fieldHead.add("更新日期时间");
		fieldHead.add("备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TBin binBean : binList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(binBean.getBinNo());
			fieldItem.add(binBean.getBinType());
			fieldItem.add(binBean.getBinCatalog());
			fieldItem.add(binBean.getBinName());
			fieldItem.add(binBean.getBinCountry());
			fieldItem.add((binBean.getBinState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(binBean.getBinDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(binBean.getBinDate(), DateUtil.TIME));
			fieldItem.add(binBean.getBinRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public IBinService getBinService() {
		return binService;
	}

	public void setBinService(IBinService binService) {
		this.binService = binService;
	}

	public TBin getBinBean() {
		return binBean;
	}

	public void setBinBean(TBin binBean) {
		this.binBean = binBean;
	}

	public List<TBin> getBinList() {
		return binList;
	}

	public void setBinList(List<TBin> binList) {
		this.binList = binList;
	}

	public String getBinNo() {
		return binNo;
	}

	public void setBinNo(String binNo) {
		this.binNo = binNo;
	}

	public String getBinNos() {
		return binNos;
	}

	public void setBinNos(String binNos) {
		this.binNos = binNos;
	}

	public String getBinType() {
		return binType;
	}

	public void setBinType(String binType) {
		this.binType = binType;
	}

	public String getBinCatalog() {
		return binCatalog;
	}

	public void setBinCatalog(String binCatalog) {
		this.binCatalog = binCatalog;
	}

	public String getBinName() {
		return binName;
	}

	public void setBinName(String binName) {
		this.binName = binName;
	}

	public String getBinCountry() {
		return binCountry;
	}

	public void setBinCountry(String binCountry) {
		this.binCountry = binCountry;
	}

	public String getBinState() {
		return binState;
	}

	public void setBinState(String binState) {
		this.binState = binState;
	}

	public Date getBinDate() {
		return binDate;
	}

	public void setBinDate(Date binDate) {
		this.binDate = binDate;
	}

	public Date getBinDateTop() {
		return binDateTop;
	}

	public void setBinDateTop(Date binDateTop) {
		this.binDateTop = binDateTop;
	}

	public Date getBinDateEnd() {
		return binDateEnd;
	}

	public void setBinDateEnd(Date binDateEnd) {
		this.binDateEnd = binDateEnd;
	}

	public String getBinRemark() {
		return binRemark;
	}

	public void setBinRemark(String binRemark) {
		this.binRemark = binRemark;
	}

}
